package com.toe.order.webapi.controller;

import com.toe.common.restful.JsonPage;
import com.toe.common.restful.JsonResult;
import com.toe.order.service.OrderGoodsService;
import com.toe.pojo.dto.order.AddCartOrderGoodsDTO;
import com.toe.pojo.dto.order.AddOrderGoodsDTO;
import com.toe.pojo.dto.order.OrderPayDTO;
import com.toe.pojo.vo.order.OrderGoodsDetailsVO;
import com.toe.pojo.vo.order.OrderGoodsSimpleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/order-goods")
@Api(tags = "商品订单模块")
public class OrderGoodsController {

    @Autowired
    OrderGoodsService orderGoodsService;

    @PostMapping("/add")
    @ApiOperation("添加新订单")
    public JsonResult<Long> addNew(@RequestBody AddOrderGoodsDTO addOrderGoodsDTO) {
        return JsonResult.ok(orderGoodsService.addNewOrder(addOrderGoodsDTO));
    }

    @PostMapping("/add-cart")
    @ApiOperation("添加购物车新订单")
    public JsonResult<Long> addCartOrder(@RequestBody AddCartOrderGoodsDTO addOrderGoodsDTO) {
        return JsonResult.ok(orderGoodsService.addCartOrder(addOrderGoodsDTO));
    }

    @GetMapping("/update/{orderId}")
    @ApiOperation("用户删除订单")
    @ApiImplicitParam(value = "订单id", name = "orderId", required = true,
            dataType = "long", example = "1")
    public JsonResult<Void> updateIsDelete(@PathVariable Long orderId) {
        orderGoodsService.deleteByOrderId(orderId);
        return JsonResult.ok();
    }

    @GetMapping("/{orderId}")
    @ApiOperation("通过订单id查询该订单")
    @ApiImplicitParam(value = "订单id", name = "orderId", required = true,
            dataType = "long", example = "1")
    public JsonResult<OrderGoodsDetailsVO> getOrderDetails(@PathVariable Long orderId) {
        return JsonResult.ok(orderGoodsService.getOrderDetails(orderId));
    }

    @GetMapping("/all/{userId}")
    @ApiOperation("该用户的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<JsonPage<OrderGoodsSimpleVO>> listAllOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderGoodsService
                .listAllOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/cancel/{userId}")
    @ApiOperation("该用户取消的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<JsonPage<OrderGoodsSimpleVO>> listCancelOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderGoodsService
                .listCancelOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/is-pay/{userId}")
    @ApiOperation("该用户已支付的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "long", example = "6"),
    })
    public JsonResult<JsonPage<OrderGoodsSimpleVO>> listPayOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderGoodsService
                .listPayOrderByUserId(userId, pageNum, pageSize));
    }

    @GetMapping("/not-pay/{userId}")
    @ApiOperation("该用户待支付的全部订单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", required = true,
                    dataType = "long", example = "1"),
            @ApiImplicitParam(value = "当前页面", name = "pageNum", required = true,
                    dataType = "int", example = "1"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true,
                    dataType = "int", example = "6"),
    })
    public JsonResult<JsonPage<OrderGoodsSimpleVO>> listNotPayOrder(
            @PathVariable Long userId, int pageNum, int pageSize) {
        return JsonResult.ok(orderGoodsService
                .listNotPayOrderByUserId(userId, pageNum, pageSize));
    }

    // http://ironknight.gnway.cc:8000/order-goods/pay/93
    // http://localhost:10005/order-goods/pay/49?returnUrl=http://www.baidu.com
//    @PostMapping(value = "/pay")
//    public void pay(@RequestBody OrderPayDTO payDTO, HttpServletResponse httpResponse) {
//        orderGoodsService.pay(payDTO, httpResponse);
//    }

    @GetMapping(value = "/pay/{orderId}")
    public void pay(
            @PathVariable("orderId") Long orderId,
            String returnUrl,
            HttpServletResponse httpResponse
    ) {
        orderGoodsService.pay(orderId, returnUrl, httpResponse);
    }

    // http://ironknight.gnway.cc/order-goods/phonePay/44
    @GetMapping(value = "/phonePay/{orderId}")
    public void phonePay(@PathVariable Long orderId, HttpServletResponse httpResponse) {
        orderGoodsService.phonePay(orderId, httpResponse);
    }

    // http://ironknight.gnway.cc:61498/order-goods/appPay/31
    @GetMapping(value = "/appPay/{orderId}")
    public void appPay(@PathVariable Long orderId, HttpServletResponse httpResponse) {
        orderGoodsService.appPay(orderId, httpResponse);
    }

    @PostMapping("/returnUrl/{orderId}")
    public void returnUrl(@PathVariable Long orderId, HttpServletRequest request) {
        System.out.println("-----开始goods同步调用controller-----");
        orderGoodsService.returnUrl(orderId, request);
    }

    // http://ironknight.gnway.cc:61498/order-goods/refund/16
    @GetMapping("/refund/{orderId}")
    public JsonResult<Void> refund(@PathVariable Long orderId) {
        orderGoodsService.refund(orderId);
        return JsonResult.ok();
    }

}

